/****************************************************
 * 创建人:   robin
 * 创建时间: 2023/8/30.030 11:19
 * 项目名称: cpy
 * 文件名称: CollectManager.java
 * 文件描述: 
 *
 * All rights Reserved, Designed By 德品软件
 * @Copyright:2023-2023
 *
 ********************************************************/
package com.dp.threadmanager;

import com.dp.config.ServiceConfig;
import com.dp.constant.CollectConstant;
import com.dp.enumeration.ReturnCodeEnum;
import com.dp.enumeration.ServerPortEnum;
import com.dp.service.HandleMessageService;
import com.dp.utils.TaskThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.concurrent.Callable;

/**
 * 包名称：com.dp.threadmanager
 * 类名称：CollectManager
 * 类描述：
 * 创建人： robin
 * 创建时间：2023/8/30.030 11:19
 */
@Slf4j
@Component
@Order(value = 1)
public class CollectManager implements CommandLineRunner {
    
    @Autowired
    private ServiceConfig serviceConfig;
    
    @Autowired
    private HandleMessageService handleMessageService;
    
    @Override
    public void run(String... args) throws Exception {
    
        if (!handleMessageService.initService().getCode().equals(ReturnCodeEnum.SUCCESS.getCode())) {
            return;
        }
        
        if (serviceConfig.isPsdpSwitch()) {
            TaskThreadPool.add(new GetPsdpTokenCallable(handleMessageService));
        }
    
        try {
            for (ServerPortEnum serverPortEnum : ServerPortEnum.values()) {
                CollectServerCallable collectServerCallable = new CollectServerCallable(handleMessageService);
                collectServerCallable.setServerPortEnum(serverPortEnum);
                TaskThreadPool.add(collectServerCallable);
            }
    
            TaskThreadPool.add(new SendTankReqCallable(handleMessageService));
            TaskThreadPool.add(new SendMqCallable(handleMessageService));
    
            for (int i = 0; i < 10; i++) {
                TaskThreadPool.add(new HandleMessageCallable(handleMessageService));
            }
    
            TaskThreadPool.add((Callable) () -> {
                while (true) {
                    handleMessageService.printStatus();
                    Thread.sleep(CollectConstant.SECOND_300);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            log.error("CollectManager start failed:{}", e.getMessage());
        }
    }
    
}
